<?php

/**
 * Contains a class to display /login.php.
 *
 * PHP Version 5
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @author   Spencer Hakim <hakims@rpi.edu>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */
 
/** @uses valueOf For safe handling of arrays. */
require_once 'function_valueof.php';
 
/**
 * A class to display /login.php.
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */ 
class PageLogin extends Page
{
    /**
     * State variable for whether to attempt to process a login or not.
     *
     * @access private
     * @var bool
     */
    private $_processLogin;
    
    /**
     * State variable for whether to log out the current user or not.
     *
     * @access private
     * @var bool
     */
    private $_logoutUser;
    
    /**
     * Variable to contain the error message text, if any.
     *
     * @access public
     * @var string
     */
    public $error;
    
    /**
     * Constructor function.  Calls the parent constructor and sets the title.
     */
    public function __construct()
    {
        parent::__construct();
        parent::setTitle('Login');
        $this->_processLogin = (count($_POST) > 0);
        $this->_logoutUser   = ($this->user->role != R_GUEST);
    }
    
    /**
     * Function to log out the currently logged in user.
     *
     * @access private
     * @return null
     */
    private function _logout()
    {
        $this->db->deleteSessionById($_SESSION['sessionid']);
        $_SESSION['sessionid'] = '';
        header('Location: ' . BASE_URL);
    }
    
    /**
     * Function to process an attempted login.
     *
     * @access private
     * @return null
     */
    private function _login()
    {
        $sessionId = $this->db->login(
            valueOf($_POST, 'username'),
            valueOf($_POST, 'password')
        );
        
        if ($sessionId !== false) {
            $_SESSION['sessionid'] = $sessionId; 
            header('Location: ' . BASE_URL);
        } else {
            $this->error = 'Incorrect login information.';
        }
    }
    
    /**
     * Function to print the login form.
     *
     * @access private
     * @return null
     */
    private function _printLoginForm()
    {
        if ($this->error != '') {
            printError($this->error, 1);
        }
        echo ' <form id="login" method="post" action="">',
            "\n",
            '  <div>', "\n",
            '   <label for="username">Email address:</label>', "\n",
            '   <input id="username" name="username" type="text" />', "\n",
            '  </div>', "\n",
            '  <div>', "\n",
            '   <label for="password">Password:</label>', "\n",
            '   <input id="password" name="password" type="password" />', "\n",
            '  </div>', "\n",
            '  <div><input type="submit" value="Log In" class="button" />', 
            '</div>', "\n",
            ' </form>', "\n",
            ' <script type="text/javascript" src="', RELATIVE_PATH,
            'scripts/loginfocus.js">',
            '</script>', "\n",
            ' <p>Don\'t have an account? <a href="', RELATIVE_PATH, 
            'register.php">Create one!</a></p>', "\n";
    }
    
    /**
     * Function to display the page.
     *
     * @access public
     * @return null
     */
    public function display()
    {
        if ($this->_logoutUser) {
            $this->_logout();
        }
        if ($this->_processLogin) {
            $this->_login();
        }
        parent::printHeader();
        $this->_printLoginForm();
        parent::printFooter();
    }
}

?>